package org.lyl.taobao.controller;

import org.lyl.taobao.api.ICommodityService;
import org.lyl.taobao.api.IUserService;
import org.lyl.taobao.api.entity.Commodity;
import org.lyl.taobao.api.entity.User;
import org.lyl.taobao.service.CommodityServiceImpl;
import org.lyl.taobao.service.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

/**
 * @Author: Re
 * @Date: 2021/3/29 20:41
 */
@WebServlet("/registerUser")
public class RegisterUserServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(InsertUserServlet.class);
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            resp.setContentType("text/html;utf-8");
            String userName = req.getParameter("userName");
            String userPassword = req.getParameter("userPassword");
            String grade = req.getParameter("grade");
            if (userName==null || "".equals(userName)) {
                req.setAttribute("msg","用户名不能为空");
                req.getRequestDispatcher("registerUser.jsp").forward(req,resp);
                return;
            }
            if (userPassword==null || "".equals(userPassword)) {
                req.setAttribute("msg","密码不能为空");
                req.getRequestDispatcher("registerUser.jsp").forward(req,resp);
                return;
            }
            User user = new User(userName,userPassword,grade);
            logger.info(user.toString());
            IUserService iUserService=new UserServiceImpl();
            ICommodityService iCommodityService = new CommodityServiceImpl();
            User user1 = iUserService.registerUser(user);
            if (user1 != null) {
                HttpSession session = req.getSession();
                session.setAttribute("user",user1);
                Cookie cookie = new Cookie("JSESSIONID", session.getId());
                cookie.setPath(req.getContextPath()+"/");
                cookie.setMaxAge(30*60);
                resp.addCookie(cookie);
                if (grade.equals("1")) {
                    List<Commodity> commodityList = iCommodityService.checkAllCommodity();
                    HttpSession session1 = req.getSession();
                    session1.setAttribute("commodityListMsg",commodityList);
                    req.getRequestDispatcher("commoditys.jsp").forward(req,resp);
                } else if(grade.equals("2")){
                    req.getRequestDispatcher("checkUser.jsp").forward(req,resp);
                } else if(grade.equals("3")){
                    List<Commodity> list = iCommodityService.checkCommodityById(user1.getUserId());
                    req.setAttribute("list",list);
                    req.getRequestDispatcher("checkCommodity.jsp").forward(req,resp);
                }
            } else {
                req.setAttribute("msg","登录失败");
                req.getRequestDispatcher("registerUser.jsp").forward(req,resp);
            }
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("代码运行出错！！！");
        }
    }
}
